﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;

/// <summary>
/// Summary description for ChiTietBaiThiDAL
/// </summary>
public class ChiTietBaiThiDAL
{
    public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<ChiTietBaiThiINFO> listAll(int MaBaiThi)
    {
        string sql = "select * from ChiTietBaiThi Where MaBaiThi = " + MaBaiThi;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<ChiTietBaiThiINFO>(dr);
    }

    public static string dsChonClient(string MaBaiThi)
    {
        string sql = "select * from ChiTietBaiThi Where MaBaiThi = " + MaBaiThi;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        StringBuilder dschon = new StringBuilder("{");
        while (dr.Read()) {
            if (dschon.Length > 1) dschon.Append(",");
            int MaCH = Convert.ToInt32(IsNull(dr["MaCH"], 0));
            string DA = Convert.ToString(IsNull(dr["DapAnChon"], ""));
            dschon.Append("\"").Append(MaCH).Append("\":\"").Append(DA).Append("\"");
        }
        return dschon.Append("}").ToString();
    }

    public static ChiTietBaiThiINFO getInfo(int MaBaiThi, int MaCH)
    {
        string sql = "select * from ChiTietBaiThi Where MaBaiThi = " + MaBaiThi +" AND MaCH = "+ MaCH;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<ChiTietBaiThiINFO>(dr);
    }

    public static int getMaChiTiet(int MaBaiThi)
    {
        string sql = "select ISNULL(Max(MaChiTiet),0) from ChiTietBaiThi Where MaBaiThi = " + MaBaiThi;
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read())
        {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }

    public static int Insert(ChiTietBaiThiINFO obj)
    {
        string sql = "INSERT INTO ChiTietBaiThi (MaChiTiet,MaBaiThi,MaCH,DapAnChon,GiaTri) ";
        sql +="VALUES (" + obj.MaChiTiet + "," + obj.MaBaiThi + ",'"+obj.DapAnChon+"',"+obj.GiaTri+")";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Insert(ChiTietBaiThiINFO obj, SqlTransaction trans)
    {
        string sql = "INSERT INTO ChiTietBaiThi (MaChiTiet,MaBaiThi,MaCH,DapAnChon,GiaTri) ";
        sql += "VALUES (" + obj.MaChiTiet + "," + obj.MaBaiThi + ","+obj.MaCH+",'" + obj.DapAnChon + "'," + obj.GiaTri + ")";
        return SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
    }

    public static int Update(ChiTietBaiThiINFO obj, SqlTransaction trans)
    {
        string sql = "UPDATE ChiTietBaiThi SET DapAnChon = '"+obj.DapAnChon+"',GiaTri = "+obj.GiaTri+" WHERE MaChiTiet = " + obj.MaChiTiet + "AND MaCH = " + obj.MaCH + " AND MaBaiThi = " + obj.MaBaiThi;
        return SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
    }
}